package com.hottv.meeting.videomeeting.module.white;

import android.util.Log;

import com.google.gson.Gson;
import com.hottv.meeting.videomeeting.common.MeetingConfig;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by chengwenqiang on 2019/5/31.
 */

public class WhiteAPI {

    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    public static final MediaType STREAM
            = MediaType.parse("application/octet-stream");
//    private static final String sdkToken = "WHITEcGFydG5lcl9pZD1GdWh2WkYwUGFoMTF0UVc4bHNSNnEwRjRwZm1HNldHU1J3VjImc2lnPWY3NTBhMTY5MDRiY2ZiMzFjNjIxZjZkN2JkYTk5MmUzNzU4OTYzNTc6YWRtaW5JZD0yMzImcm9sZT1taW5pJmV4cGlyZV90aW1lPTE1OTA2NjE3MzYmYWs9RnVodlpGMFBhaDExdFFXOGxzUjZxMEY0cGZtRzZXR1NSd1YyJmNyZWF0ZV90aW1lPTE1NTkxMDQ3ODQmbm9uY2U9MTU1OTEwNDc4Mzg3NzAw";
    public static String sdkToken = "WHITEcGFydG5lcl9pZD1PUXJrRkRYSUtsVDAxNG9qY1BrWXhOM0VUSEtFYWNlQU9BelYmc2lnPWQ4YWE3ZTQ3YTVkNDcyMDEyMTM5Nzg3MzM3MmIxMzU3MzFiMmFjN2Q6YWRtaW5JZD0yMzcmcm9sZT1taW5pJmV4cGlyZV90aW1lPTE1OTA3NDAzNDAmYWs9T1Fya0ZEWElLbFQwMTRvamNQa1l4TjNFVEhLRWFjZUFPQXpWJmNyZWF0ZV90aW1lPTE1NTkxODMzODgmbm9uY2U9MTU1OTE4MzM4NzkwMDAw";
    private static final String host = "https://cloudcapiv4.herewhite.com";

//    private static final String up_load_host = "http://fileyidonghottv.bmobpay.com/"; https://apiipvt.bmobpay.com
    //闭幕host
    private static final String bmop_host = "https://apiipvt.bmobpay.com/";
    private OkHttpClient client =null ;
    private Gson gson = null;

    private WhiteAPI()
    {
        client = new OkHttpClient();
        gson = new Gson();
    }
    private static WhiteAPI instance;

    public static  WhiteAPI getInstance()
    {
        if(instance==null)
        {
            instance = new WhiteAPI();
        }
        return   instance;
    }

    public boolean validateToken() {
        return sdkToken.length() > 200;
    }

    public void createRoom(String name, int limit, Callback callback) {
        Map<String, Object> roomSpec = new HashMap<>();
        roomSpec.put("name", name);
        roomSpec.put("limit", limit);
        RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
        Request request = new Request.Builder()
                .url(host + "/room?token=" + sdkToken)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    public void getRoomToken(String uuid, Callback callback) {
        Map<String, Object> roomSpec = new HashMap<>();
        RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
        Request request = new Request.Builder()
                .url(host + "/room/join?uuid=" + uuid + "&token=" + sdkToken)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    //上传文件
    public void uploadFile(String url,String filePath,Callback callback)
    {
        File file = new File(filePath);
        if(!file.exists())
        {
            Log.i(MeetingConfig.TAG,"file is not exit filePath:"+filePath);
            return;
        }


        String fileName =filePath.substring(filePath.lastIndexOf("/")+1);
        try{
            fileName = URLEncoder.encode(fileName,"utf-8");
        }catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        Log.i(MeetingConfig.TAG,"url:"+url+"1/files/"+""+fileName);
        Log.i(MeetingConfig.TAG,"filepath:"+filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse(WhiteUtils.getMimeType(filePath)), file);

//        RequestBody requestBody = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM)   // multipart/form-data
//                .addFormDataPart("file", file.getName(), requestFile)
//                .build();
        Request request = new Request.Builder()
                .url(url+"1/files/"+""+fileName)
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    public void documentToPNG(String sourceUrl,String folder,Callback callback)
    {
        Log.i(MeetingConfig.TAG,"documentToPNG");
        Map<String, Object> roomSpec = new HashMap<>();
        roomSpec.put("sourceUrl", sourceUrl);
        roomSpec.put("targetBucket", "hottv");
        roomSpec.put("targetFolder", folder);
        RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
        Request request = new Request.Builder()
                .url(host + "/services/static-conversion/tasks?token=" + sdkToken)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    public void getDocumentInfo(String taskid,Callback callback)
    {
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(host+"/services/static-conversion/tasks/"+taskid+"/progress?token="+sdkToken)
                .build();

        Call call = client.newCall(request);
        call.enqueue(callback);

    }

    // 获取herewhite的MiniToken
    public void getMiniToken(Callback callback)
    {
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(bmop_host+"1/miniToken")
                .build();

        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    //比目科技创建房间
    public void bmopCreateRoom(String roomName,Callback callback)
    {
        Map<String, Object> roomSpec = new HashMap<>();
        roomSpec.put("name", roomName);
        RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
        Request request = new Request.Builder()
                .url(bmop_host + "1/meeting")
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    //比目科技获取房间信息
    public void getBmopRoomInfo(String roomName,Callback callback)
    {
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(bmop_host+"1/meeting/"+roomName)
                .build();

        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    static String TEST_UUID = "3e1f1b3704b8490880b696c081bbaf6f";
    static String TEST_ROOM_TOKEN = "WHITEcGFydG5lcl9pZD1GdWh2WkYwUGFoMTF0UVc4bHNSNnEwRjRwZm1HNldHU1J3VjImc2lnPWUzNDQzNjg0ZGJiN2EyNTQyMGRjODNkNmIxM2Q1NGI3MDgxZDQ5MDk6YWRtaW5JZD0yMzImcm9vbUlkPTNlMWYxYjM3MDRiODQ5MDg4MGI2OTZjMDgxYmJhZjZmJnRlYW1JZD0zNTcmcm9sZT1yb29tJmV4cGlyZV90aW1lPTE1OTA2NjU2MTYmYWs9RnVodlpGMFBhaDExdFFXOGxzUjZxMEY0cGZtRzZXR1NSd1YyJmNyZWF0ZV90aW1lPTE1NTkxMDg2NjQmbm9uY2U9MTU1OTEwODY2Mzg3ODAw";
}

